package net.anjingnorak.javabase.connection;

import java.io.File;
import java.sql.DriverManager;

public class Connection {
	public static java.sql.Connection dbConnection;
	public static String MYSQL = "mysql";
	public static String SQLSERVER = "sqlserver";
	public static String PostgreSQL = "postgresql";
	public static String SQLLITE = "sqllite";
	
	private static String connectionType;
	private static String connectionHost;
	private static String connectionName;
	private static String connectionUsername;
	private static String connectionPassword;
	private static File connectionFile;
	
	/**
	 * Menghubungkan aplikasi dengan database dengan pengembalian data berupa boolean true = berhasil dan false = gagal
	 * NB : harus mengisikan connectionType, connectionHost, connectionName, connectionUsername, conectionPassword, connectionFile terlebih dahulu 
	 * 
	 * @return boolean
	 * @throws Exception
	 */
	public static boolean connectToDatabase() throws Exception{
		String url = "";
		
		if(connectionType.equalsIgnoreCase(MYSQL)){
			Class.forName("com.mysql.jdbc.Driver");
			url = "jdbc:mysql://" + connectionHost + "/" + connectionName;
		}else if(connectionType.equalsIgnoreCase(SQLSERVER)){
			Class.forName("net.sourceforge.jtds.jdbc.Driver");
			url = "jdbc:jtds:sqlserver://" + connectionHost + "/" + connectionName;
		}else if(connectionType.equalsIgnoreCase(SQLLITE)){
			Class.forName("org.sqlite.JDBC");
			url = "jdbc:sqlite:"+connectionFile.getPath();
		}else if(connectionType.equalsIgnoreCase(PostgreSQL)){
			Class.forName("org.postgresql.Driver");
			url = "jdbc:postgresql://" + connectionHost + "/" + connectionName;
		}
		
		if(!url.equalsIgnoreCase("")){
			dbConnection = DriverManager.getConnection(url, connectionUsername, connectionPassword);
			return true;
		}
		
		return false;
	}
	
	/**
	 * Berfungsi untuk melakukan Begin Trans sebelum transaksi
	 * NB : Bisa dilakukan apabila database men-support begin trans
	 */
	public static void beginTran(){
		try {
			dbConnection.setAutoCommit(false);
		} catch (Exception e) {
			System.err.println(e);
		}
	}
	
	/**
	 * Berfungsi untuk melakukan Commit setelah transaksi dilakukan
	 * NB : Bisa dilakukan apabila database men-support commit
	 */
	public static void commit(){
		try {
			dbConnection.commit();
		} catch (Exception e) {
			System.err.println(e);
		}
	}
	
	/**
	 * Berfungsi untuk melakukan rollback setelah transaksi bila ada terjadi kegagalan
	 * NB : Bisa dilakukan apabila database men-support rollback
	 */
	public static void rollback(){
		try {
			dbConnection.rollback();
		} catch (Exception e) {
			System.err.println(e);
		}
	}

	public static String getConnectionType() {
		return connectionType;
	}

	public static void setConnectionType(String connectionType) {
		Connection.connectionType = connectionType;
	}

	public static String getConnectionHost() {
		return connectionHost;
	}

	public static void setConnectionHost(String connectionHost) {
		Connection.connectionHost = connectionHost;
	}

	public static String getConnectionName() {
		return connectionName;
	}

	public static void setConnectionName(String connectionName) {
		Connection.connectionName = connectionName;
	}

	public static String getConnectionUsername() {
		return connectionUsername;
	}

	public static void setConnectionUsername(String connectionUsername) {
		Connection.connectionUsername = connectionUsername;
	}

	public static String getConnectionPassword() {
		return connectionPassword;
	}

	public static void setConnectionPassword(String connectionPassword) {
		Connection.connectionPassword = connectionPassword;
	}

	public static File getConnectionFile() {
		return connectionFile;
	}

	public static void setConnectionFile(File connectionFile) {
		Connection.connectionFile = connectionFile;
	}	
}
